import { GetSession, LoginOut } from '@/api/user'
import { parfn } from '@/utils/requestpar'
import { ElMessage } from 'element-plus'
import settings from '@/config/index'
const state = {
  USearch: {},
}
const getters = {}
const mutations = {
  setUSearch(state, USearch) {
    state.USearch = USearch
  },
}
const actions = {
  async GetSession({ commit, state }) {
    const { data } = await GetSession(state.accessToken)
    if (data.length == 1) {
      commit('setUSearch', data[0])

      return data[0]
    } else {
      return {}
    }
  },
  async LoginOut() {
    const { data } = await LoginOut(state.accessToken)
    let par = parfn(data)
    if (par.value == 'succeed') {
      window.location.href = settings.LoginOuturl ? settings.LoginOuturl : '/'
    } else {
      ElMessage.error(par.label)
    }
  },
}

export default { state, getters, mutations, actions }
